package team.knowers.qzh.qzhadmin.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import team.knowers.qzh.qzhadmin.dao.entity.CourseDO;
import team.knowers.qzh.qzhadmin.dao.mapper.CourseMapper;
import team.knowers.qzh.qzhadmin.dto.req.PageCourseReqDTO;
import team.knowers.qzh.qzhadmin.dto.req.PageCourseReqDTO1;
import team.knowers.qzh.qzhadmin.dto.resp.CourseMessageRespDTO;
import team.knowers.qzh.qzhadmin.dto.resp.CoursePageRespDTO;
import team.knowers.qzh.qzhadmin.dto.resp.PageCourseMessageRespDTO;
import team.knowers.qzh.qzhadmin.service.CourseAdminService;

@Service
@AllArgsConstructor
public class CourseAdminServiceImpl extends ServiceImpl<CourseMapper,CourseDO> implements CourseAdminService{
    @Override
    public String examineCourse(Long id) {
        LambdaQueryWrapper<CourseDO> queryWrapper = Wrappers.lambdaQuery(CourseDO.class)
                .eq(CourseDO::getId, id)
                .eq(CourseDO::getDelFlag, 0);
        CourseDO courseDO = baseMapper.selectOne(queryWrapper);
        courseDO.setStatus(2);
        baseMapper.update(courseDO, queryWrapper);
        return (courseDO.getName() + "通过审核");

    }
    /**
     * 课程审核不通过
     */
    @Override
    public String notExamineCourse(Long id) {
        LambdaQueryWrapper<CourseDO> queryWrapper = Wrappers.lambdaQuery(CourseDO.class)
                .eq(CourseDO::getId, id)
                .eq(CourseDO::getDelFlag, 0);
        CourseDO courseDO = baseMapper.selectOne(queryWrapper);
        courseDO.setStatus(0);
        baseMapper.update(courseDO, queryWrapper);
        return (courseDO.getName() + "未通过审核");

    }

    @Override
    public IPage<PageCourseMessageRespDTO> showCourse(PageCourseReqDTO req) {
        if(req.getId() == null&&req.getName() == null&&(req.getStatus()== null || req.getStatus() == 3))
        {
            LambdaQueryWrapper<CourseDO> queryWrapper1=Wrappers.lambdaQuery(CourseDO.class).eq(CourseDO::getDelFlag,0);
           IPage<CourseDO> courseDO=baseMapper.selectPage(req,queryWrapper1);
            return courseDO.convert(each -> BeanUtil.toBean(each, PageCourseMessageRespDTO.class));
        }
        else {
            LambdaQueryWrapper<CourseDO> queryWrapper = new LambdaQueryWrapper<>();

            if (req.getId() != null) {
                queryWrapper.like(CourseDO::getId, "%" + req.getId() + "%");
            }
            if (req.getName() != null) {
                queryWrapper.like(CourseDO::getName, "%" + req.getName() + "%");
            }
            if(req.getDescription()!=null)
            {
                queryWrapper.like(CourseDO::getDescription,"%" + req.getDescription()+ "%");
            }
            if (req.getStatus() != null && req.getStatus() != 3) {
                queryWrapper.eq(CourseDO::getStatus, req.getStatus());
            }
            // 筛选查询条件
            IPage<CourseDO> courseDOIPage = baseMapper.selectPage(req, queryWrapper);
            return courseDOIPage.convert(each -> BeanUtil.toBean(each, PageCourseMessageRespDTO.class));
        }

    }
    @Override
    public IPage<CoursePageRespDTO> pageCourse(PageCourseReqDTO1 req) {
        LambdaQueryWrapper<CourseDO> queryWrapper = Wrappers.lambdaQuery(CourseDO.class).eq(CourseDO::getUserId, req.getUserId())
                .eq(CourseDO::getDelFlag, 0)
                .orderByAsc(CourseDO::getCreateTime);
        IPage<CourseDO> coursePageRespDTOIPage = baseMapper.selectPage(req, queryWrapper);
        return coursePageRespDTOIPage.convert(each -> BeanUtil.toBean(each, CoursePageRespDTO.class));
    }
    @Override
    public CourseMessageRespDTO showCourse(Long id) {
        LambdaQueryWrapper<CourseDO> queryWrapper=Wrappers.lambdaQuery(CourseDO.class)
                .eq(CourseDO::getId,id)
                .eq(CourseDO::getDelFlag,0);
        CourseDO courseDO=baseMapper.selectOne(queryWrapper);
        return BeanUtil.toBean(courseDO, CourseMessageRespDTO.class);
    }

}


